package org.community.test.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
@Slf4j
public class FileUtil {
    //文件重命名
    public Map<String,String> fileNameRename(String fileName){
        String[] nameParts = fileName.split("\\.");
        Map<String,String> nameResultMap = new HashMap<>();
        nameResultMap.put("suffix",nameParts[nameParts.length-1]);
        nameResultMap.put("prefix", UUID.randomUUID().toString());
        return nameResultMap;
    }

    //文件名前后缀解析
    public Map<String,String> fileNameTakeApart(String fileName){
        String[] nameParts = fileName.split("\\.");
        Map<String,String> nameResultMap = new HashMap<>();
        nameResultMap.put("suffix",nameParts[nameParts.length-1]);
        StringBuilder prefix = new StringBuilder();
        for(int index = 0;index<nameParts.length-1;index++){
            prefix.append(nameParts[index]);
        }
        //切片编号过滤
        String[] prefixStepTwo = prefix.toString().split("_");
        log.info("切分的字符串组：{}", (Object) prefixStepTwo);
        if(prefixStepTwo.length !=0 ){
            prefix = new StringBuilder();
            if(prefixStepTwo.length > 1){
                for(int index = 0;index<prefixStepTwo.length-1;index++){
                    prefix.append(prefixStepTwo[index]);
                }
            }else {
                prefix.append(prefixStepTwo[0]);
            }

        }

        nameResultMap.put("prefix",prefix.toString());
        log.info("prefix:{}",nameResultMap.get("prefix"));
        log.info("suffix:{}",nameResultMap.get("suffix"));
        return nameResultMap;
    }


    //新建缓存目录
    public String makeCacheDir(String folderName,String cacheDirName){
        String str = System.getProperty("user.dir");
        String sap = System.getProperty("file.separator");
        String[] string;
        if(sap.equals("\\")){
            string = str.split("\\\\");
        }else {
            string = str.split(sap);
        }

        System.out.println(str);
        StringBuilder new_str = new StringBuilder();
        for(int i = 0; i< string.length; i++){
            new_str.append(string[i]).append(sap);
        }
//        String folderPath = new_str.toString() + cacheDirName "video_cache";
        String folderPath = new_str.toString() + cacheDirName ;
        File file1 = new File(folderPath);
        if(!file1.exists()){
            file1.mkdir();
        }
        folderPath = folderPath + sap + folderName;
        File file2 = new File(folderPath);
        if(!file2.exists()){
            file2.mkdir();
        }
        return folderPath + sap;
    }


    // file转MultiPartFile
    public FileItem getMultipartFile(File file, String fieldName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
        int bytesRead = 0;
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }

    //读取文件夹内所有内容
    public Map<String,File> getFolderFileList(String folderPath){
        List<File> fileList = cn.hutool.core.io.FileUtil.loopFiles(folderPath);
        Map<String,File> fileMap = new HashMap<>();
        for(File file:fileList){
            fileMap.put(file.getName(),file);
        }

            return fileMap;
    }

}
